package com.icookie.shuatim.api.exception;

import com.shuatim.common.exception.ShuaTiException;
import com.shuatim.common.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author : issuser ✌️
 * @date : 2019/4/17 13:57
 */
@RestControllerAdvice
@Slf4j
public class ShuatiExceptionHandler {

    /**
     * 处理自定义异常
     */
    @ExceptionHandler(ShuaTiException.class)
    public Result handleRRException(ShuaTiException e) {

        /*Result r = new Result();
        r.put("code", e.getCode());
        r.put("errorMsg", e.getMessage());
        log.error(e.getMessage(), e, r);*/
        log.error(e.getMessage(), e);
        return Result.error(e.getCode(), e.getMessage(), false);
    }

    @ExceptionHandler(DuplicateKeyException.class)
    public Result handleDuplicateKeyException(DuplicateKeyException e) {
        log.error(e.getMessage(), e);
        return Result.error("数据库中已存在该记录");
    }

    @ExceptionHandler(Exception.class)
    public Result handleException(Exception e) {
        log.error(e.getMessage(), e);
        return Result.error();
    }
}
